home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
wstr.exe
/
WSTR.C
< prev
next >
Wrap
Text File
|
1993-02-26
|
22KB
|
1,182 lines
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <WStr.h>
#pragma hdrstop
// copyright (c) 1992, 1993 by Paul Wheaton
//.parse
///////// BaseString class
static char GarbageChar;
char& BaseString::operator[](int Index)
{
if (Index >= Len)
{
GarbageChar=0;
return GarbageChar;
}
return P[Index];
}
/*
The idea here is that if a programmer were to give an index out of range
then they won't be messing up something. If they wrote "S[5]='X'" and S
was a string of length 2, then GarbageChar would be set to 'X' and S would
be unaffected. "MyChar=S[5]" would result in MyChar being assigned 0.
*/
//.parse
Bool BaseString::operator==(const BaseString& S) const
{
if (Len != S.Len) return No;
int I = Len/(sizeof(int));
const int* P1 = (const int*)P;
const int* P2 = (const int*)S.P;
while (I!=0)
{
if (*P1 != *P2) return No;
P1++;
P2++;
I--;
}
I= Len%(sizeof(int));
if (I!=0)
{
const char* PP1=(const char*)P1;
const char* PP2=(const char*)P2;
while (I!=0)
{
if (*PP1 != *PP2) return No;
PP1++;
PP2++;
I--;
}
}
return Yes;
}
//.parse
void BaseString::ToLower()
{
int I = Len;
char* Q = P;
while (I--) { *Q = tolower(*Q); Q++; }
}
//.parse
void BaseString::ToUpper()
{
register int I = Len;
register char* Q = P;
while (I--) { *Q = toupper(*Q); Q++; }
}
//.parse
int BaseString::Index(char SearchChar, int StartIndex) const
{
if (StartIndex>=Len) return(NotFound);
int I=StartIndex;
while ((I<Len) && (P[I]!=SearchChar)) I++;
if (I==Len) return(NotFound);
else return(I);
}
//.parse
int BaseString::Index(const char* SearchStr, int StartIndex) const
{
if (StartIndex>=Len) return(NotFound);
char* Pos=strstr(P+StartIndex,SearchStr);
if (Pos==NULL) return NotFound;
else return int(Pos-P);
}
//.parse
int BaseString::Count(char C) const
{
int Num=0;
int I;
For(I,Len) if(P[I]==C) Num++;
return Num;
}
//.parse
void BaseString::Delete(int Index,int Length)
{
// *this=BeforeSub(Index)+FromSub(Index+Length);
int S=Index+Length;
while (S<Len)
{
P[Index]=P[S];
Index++;
S++;
}
P[Index]='\0';
Len=Index;
}
//.parse
void BaseString::DeleteLast()
{
if (Len>0)
{
Len--;
P[Len]='\0';
}
}
//.parse
void BaseString::Trim()
{
if (Len==0) return;
int I=0;
while ((P[I]==' ')&&(I<Len)) I++;
if (I>0) Delete(0,I);
if (Len==0) return;
TrimTrail();
}
//.parse
void BaseString::TrimTrailTo(char C)
{
if (Len>0)
{
Len--;
while ((P[Len]!=C)&&(Len>0)) Len--;
P[Len]='\0';
}
}
//.parse
void BaseString::TrimTrail()
{
if (Len>0)
{
Len--;
while ((P[Len]==' ')&&(Len>0)) Len--;
Len++;
P[Len]='\0';
}
}
//.parse
char BaseString::At(int Index) const
{
if (Index>=Len) return '\0';
return (P[Index]);
}
//.parse
char BaseString::Last() const
{
if (Len==0) return '\0';
else return P[Len-1];
}
/////////// String class
//.parse
void String::ReNew(int NewCapacity) // replaces ANSI-C realloc
{
#ifdef MAJORBBS
char* P2=(char*)malloc(NewCapacity);
#else
char* P2=new char[NewCapacity];
#endif
if (P2==NULL) FatalError("out of string mem rn");
strncpy(P2,P,NewCapacity-1);
#ifdef MAJORBBS
free(P);
#else
delete(P);
#endif
P=P2;
P[NewCapacity-1]=0;
}
void String::New()
{
#ifdef MAJORBBS
P = (char*)malloc(Alloc);
#else
P = new char[Alloc];
#endif
if (P==NULL) FatalError("out of string mem");
}
//.parse
String::String(const char& C, int L, int Extra)
{
Len = L;
Alloc = Len + Extra + 1;
New();
register int I=Len;
P[I] = 0;
while (I) P[--I] = C;
}
//.parse
String::String(int Extra)
{
Len = 0;
Alloc = Extra + 1;
New();
*P = 0;
}
//.parse
String::String(const char* CS, int Extra)
{
Len = strlen(CS);
Alloc = Len + Extra + 1;
New();
strcpy(P,CS);
}
//.parse
String::String(const BaseString& S, int Extra)
{
Len = S.Len;
Alloc = Len + Extra + 1;
New();
strcpy(P,S.P);
}
//.parse
void String::operator=(const BaseString& S)
{
if (P == S.P) return;
Len = S.Len;
if (Len >= Alloc)
{
#ifdef MAJORBBS
free(P);
#else
delete(P);
#endif
Alloc = S.Alloc;
New();
}
strcpy(P,S.P);
}
//.parse
void String::operator=(const char* CS)
{
Len = strlen(CS);
if (Len >= Alloc)
{
Alloc = Len + DefaultStringExtra + 1;
#ifdef MAJORBBS
free(P);
#else
delete(P);
#endif
New();
}
strcpy(P,CS);
}
//.parse
void String::operator=(const char C)
{
Len=1;
P[0]=C;
}
//.parse
String String::operator+(const String40& S) const
{
String T(Len+S.Len+1);
strcpy(T.P,P);
strcpy(&(T.P[Len]), S.P);
T.Len = Len+S.Len;
return(T);
}
//.parse
String String::operator+(const String120& S) const
{
String T(Len+S.Len+1);
strcpy(T.P,P);
strcpy(&(T.P[Len]), S.P);
T.Len = Len+S.Len;
return(T);
}
//.parse
String String::operator+(const String& S) const
{
String T(Len+S.Len+1);
strcpy(T.P,P);
strcpy(&(T.P[Len]), S.P);
T.Len = Len+S.Len;
return(T);
}
//.parse
String String::operator+(const char* CS) const
{
int CSLen = strlen(CS);
String T(Len+CSLen+1);
strcpy (T.P,P);
strcpy (&(T.P[Len]), CS);
T.Len = Len+CSLen;
return T;
}
//.parse
String String::operator+(char C) const
{
String S(*this,1);
S.P[Len]=C;
S.Len++;
S.P[S.Len]=0;
return S;
}
//.parse
String operator+(const String40& SS, const String& S)
{
String T(SS.Length()+S.Len+1);
strcpy(T.P,(const char*)SS);
strcpy(&(T.P[SS.Length()]), S.P);
T.Len = SS.Length()+S.Len;
return(T);
}
//.parse
String operator+(const String120& SS, const String& S)
{
String T(SS.Length()+S.Len+1);
strcpy(T.P,(const char*)SS);
strcpy(&(T.P[SS.Length()]), S.P);
T.Len = SS.Length()+S.Len;
return(T);
}
//.parse
String operator+(const char* CS, const String& S)
{
int CSLen=strlen(CS);
String T(int(CSLen+S.Len+1));
strcpy(T.P,CS);
strcpy(&(T.P[CSLen]),S.P);
T.Len = CSLen + S.Len;
return T;
}
//.parse
String operator+(char C, const String& S)
{
int NewLen=S.Len+1;
String T(NewLen);
T.P[0]=C;
strcpy(&(T.P[1]),S.P);
T.Len = NewLen;
return T;
}
//.parse
void String::operator+=(const BaseString& S)
{
if (S.Len==0) return;
int NewLen=Len+S.Len;
if (Alloc <= NewLen)
{
Alloc = NewLen+1;
ReNew(Alloc);
}
strcpy(&P[Len],S.P);
Len += S.Len;
}
//.parse
void String::operator+=(const char* CS)
{
int CSLen = strlen(CS);
if (CSLen==0) return;
int NewLen=Len+CSLen;
if (Alloc <= NewLen)
{
Alloc = NewLen+1;
ReNew(Alloc);
}
strcpy(&(P[Len]),CS);
Len += CSLen;
}
void String::operator+=(char C)
{
*this=*this+C;
/*
if (Alloc < Len+2)
{
Alloc += (DefaultStringExtra+1);
ReNew(Alloc);
}
P[Len]=C;
Len++;
P[Len]=0;
*/
}
//.parse
void String::Left(int NewSize)
{
if (Len>NewSize)
{
Len=NewSize;
P[